package ce.com.newbluedemo.k2;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.NumberPicker;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ce.com.cenewbluesdk.entity.k2.K2_SendAlarmInfoStruct;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/28.
 */

public class ActivityAsyncAlarmClock extends K2BaseActivity implements RadioGroup.OnCheckedChangeListener, NumberPicker.OnValueChangeListener, CompoundButton.OnCheckedChangeListener {
    private CEDevK2Proxy mK2Proxy;
    private NumberPicker mHourPicker, mMinutePicker;
    private int mHour;
    private int mMinute;
    private int week_repeat;
    private int switchState;
    private ArrayList<K2_SendAlarmInfoStruct> mClocks;
    private Handler mK2Handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0) {
                showToast("发送失败，请稍后重试！");
            } else {
                showToast("发送成功！");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_alarm_clock);
        setTitle("同步闹钟");
        mK2Proxy = BleFactory.getInstance().getK2Proxy();
        mK2Proxy.setHandler(mK2Handler);
        mClocks = new ArrayList<>();
        ((RadioGroup) findViewById(R.id.rg_repeat_day)).setOnCheckedChangeListener(this);
        mHourPicker = ((NumberPicker) findViewById(R.id.np_hour));
        mHourPicker.setOnValueChangedListener(this);
        mHourPicker.setMaxValue(23);
        mHourPicker.setMinValue(0);
        mMinutePicker = ((NumberPicker) findViewById(R.id.np_minute));
        mMinutePicker.setOnValueChangedListener(this);
        mMinutePicker.setMinValue(0);
        mMinutePicker.setMaxValue(59);
        ((Switch) findViewById(R.id.sw_alarm_switch)).setOnCheckedChangeListener(this);

        findViewById(R.id.btn_async_alarm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mK2Proxy.isConnectOK()) {
                    showToast("蓝牙已经断开");
                    return;
                }
                mClocks.clear();
                mClocks.add(new K2_SendAlarmInfoStruct((byte) week_repeat, (byte) mHour, (byte) mMinute, (byte) switchState));
                mK2Proxy.getSendHelper().sendAlarmInfo(mClocks);
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_repeat_day:
                switch (checkedId) {
                    case R.id.rb_sunday:
                        week_repeat = 0;
                        break;
                    case R.id.rb_mon:
                        week_repeat = 1;
                        break;
                    case R.id.rb_tuesday:
                        week_repeat = 2;
                        break;
                    case R.id.rb_wednesday:
                        week_repeat = 3;
                        break;
                    case R.id.rb_thursday:
                        week_repeat = 4;
                        break;
                    case R.id.rb_fri:
                        week_repeat = 5;
                        break;
                    case R.id.rb_sat:
                        week_repeat = 6;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.np_hour:
                mHour = newVal;
                break;
            case R.id.np_minute:
                mMinute = newVal;
                break;
            default:
                break;
        }
    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.sw_alarm_switch) {
            switchState = isChecked ? 0 : 1;
        }
    }
}
